package com.ts.oss.controller;


import com.aliyun.oss.ClientException;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.vod.model.v20170321.CreateUploadImageRequest;
import com.aliyuncs.vod.model.v20170321.CreateUploadImageResponse;
import com.aliyuncs.vod.model.v20170321.GetImageInfoRequest;
import com.aliyuncs.vod.model.v20170321.GetImageInfoResponse;
import com.ts.oss.service.VodService;
import com.ts.oss.util.ConstantPropertiesUtils;
import com.ts.oss.util.InitVodClient;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.rmi.ServerException;
import java.util.HashMap;
import java.util.Map;

@Api(tags="视频管理")
@RestController
@RequestMapping("/oss/eduvod/video")
public class VodController {

    @Resource
    private VodService vodService;


    //上传视频到阿里云
    @ApiOperation("uploadVideo")
    @PostMapping("uploadVideo")
    public Map<String,Object> uploadVideo(MultipartFile multipartFile)
    {
        String videoId=vodService.uploadVideo(multipartFile);
        HashMap<String, Object> map = new HashMap<>();
        map.put("code","0");
        map.put("message","成功");
        HashMap<String, Object> data = new HashMap<>();
        data.put("videoId",videoId);
        map.put("data",data);
        return map;
    }

    //上传视频的同时将视频的某一帧作为封面上传
    @ApiOperation("上传视频并取某帧作为视频的封面")
    @PostMapping("upload")
    public Map<String,Object> uploadVideoAndImage(@RequestParam("file") MultipartFile multipartFile)
    {
       String[] strings=vodService.uploadVideoAndImage(multipartFile);
        HashMap<String, Object> map = new HashMap<>();
        map.put("code","0");
        map.put("message","成功");
        map.put("data",strings);
       System.out.println(strings);

       return map;
    }

    //根据id获取视频凭证
    @GetMapping("getPlayAuth/{id}")
    public Map<String,Object> getPlayIdAndAuth(
            @PathVariable String id
    ){
        String s= vodService.getPlayIdAndAuth(id);
        HashMap<String, Object> map = new HashMap<>();
        map.put("code","0");
        map.put("message","成功");
        HashMap<String, Object> data = new HashMap<>();
        data.put("auth",s);
        map.put("data",data);
        return map;

    }

    //图片的url是一直更换的 因此需要存入图片的id在使用时查找当前的图片地址
    @ApiOperation("根据图片id获取图片地址")
    @GetMapping("getImageUrl/{id}")
    public String getImageUrl(
            @PathVariable String id
    ){
        DefaultAcsClient acsClient = InitVodClient.initVodClient(ConstantPropertiesUtils.KEY_ID, ConstantPropertiesUtils.KEY_SECRET);
        GetImageInfoRequest request = new GetImageInfoRequest();
        request.setImageId(id);
        GetImageInfoResponse response;

        try {
             response = acsClient.getAcsResponse(request);
            // System.out.println(response.getImageInfo().getURL());
             return response.getImageInfo().getURL();

        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }



    }





}
